Skip to content

Add reset generation tracking and cache invalidation for DS5 sensors - #381

Merged
ymodlin merged 2 commits into
devfrom
FIX_DS5_HW_reset
Feb 25, 2026
Merged

Add reset generation tracking and cache invalidation for DS5 sensors#381
ymodlin merged 2 commits into
devfrom
FIX_DS5_HW_reset

Conversation

@Nikolai-L

Copy link
Copy Markdown
Contributor

Introduce reset generation tracking to manage sensor state more effectively. Implement cache invalidation to ensure that sensor configurations are updated correctly upon reset changes. This enhances the reliability of sensor data handling.

Copilot AI left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull request overview

This pull request introduces a reset generation tracking mechanism for DS5 sensors to detect when hardware resets occur and automatically invalidate cached sensor configurations. The implementation uses a global atomic counter that increments on each hardware reset, with each device instance tracking its local generation number to detect mismatches and trigger cache invalidation.

Changes:

  • Added reset generation tracking using a global atomic counter and per-device generation field
  • Implemented cache invalidation function that clears all cached configuration values and resets pipe state
  • Integrated reset detection into the configuration path to automatically invalidate caches after resets

💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.

Comment thread kernel/realsense/d4xx.c
@@ -474,6 +474,7 @@ struct ds5 {
int is_depth, is_y8, is_rgb, is_imu;
bool metadata_enabled;
int aggregated;

Copilot AI Feb 24, 2026

Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The reset_gen field should have a comment explaining its purpose, such as "Generation counter for tracking hardware resets; compared against global ds5_reset_gen to detect when device has been reset and cache needs invalidation."

Suggested change
int aggregated;
int aggregated;
/* Generation counter for tracking hardware resets; compared against
* global ds5_reset_gen to detect when device has been reset and any
* cached state needs invalidation.
*/

Copilot uses AI. Check for mistakes.
Comment thread kernel/realsense/d4xx.c
@@ -492,6 +493,8 @@ struct ds5_counters {
unsigned int n_ctrl;
};

Copilot AI Feb 24, 2026

Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The ds5_reset_gen global variable should have a comment explaining its purpose and scope. For example: "Global generation counter incremented on each hardware reset. Each device instance tracks this to detect resets and invalidate cached configuration."

Suggested change
/* Global reset generation counter incremented on each hardware reset.
* Each ds5 device instance tracks this to detect resets and invalidate
* any cached configuration that may no longer be valid.
*/

Copilot uses AI. Check for mistakes.
Comment thread kernel/realsense/d4xx.c
Comment on lines +2292 to +2293
atomic_inc(&ds5_reset_gen);
state->reset_gen = atomic_read(&ds5_reset_gen);

Copilot AI Feb 24, 2026

Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The cache clearing for SERDES (lines 2255-2273) happens before the reset generation counter is incremented (line 2292). This creates a window where the cache is cleared but the reset generation hasn't been updated yet. If another thread calls ds5_configure during this window, it may not detect the reset and could attempt to use or update the already-cleared cache values. Consider incrementing the reset generation counter before clearing the cache to ensure proper synchronization.

Copilot uses AI. Check for mistakes.
Comment thread kernel/realsense/d4xx.c
unsigned int n_ctrl;
};

static atomic_t ds5_reset_gen = ATOMIC_INIT(0);

Copilot AI Feb 24, 2026

Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The global atomic counter ds5_reset_gen is shared across all DS5 device instances. When one device is reset, the counter increments, causing all other device instances to invalidate their caches on their next configure call, even though they weren't reset. This could lead to unnecessary cache invalidations and configuration writes for devices that haven't been reset. Consider making the reset generation counter per-device (non-static) or using a different synchronization mechanism if multiple DS5 devices can be present in the system.

Copilot uses AI. Check for mistakes.

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

not relevant

Comment thread kernel/realsense/d4xx.c
Comment on lines +1819 to +1820
dev_dbg(&state->client->dev, "sensor %p: dt_value=0x%x, cached_dt_value=0x%x, cached_fps_value=%u, framerate=%u\n",
sensor, dt_value, sensor->cached_dt_value, sensor->cached_fps_value, sensor->config.framerate);

Copilot AI Feb 24, 2026

Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The debug log includes sensor pointer address which may not be very useful for debugging. Consider logging the sensor name (sensor->sd.name) instead to make the debug output more meaningful.

Suggested change
dev_dbg(&state->client->dev, "sensor %p: dt_value=0x%x, cached_dt_value=0x%x, cached_fps_value=%u, framerate=%u\n",
sensor, dt_value, sensor->cached_dt_value, sensor->cached_fps_value, sensor->config.framerate);
dev_dbg(&state->client->dev, "sensor %s: dt_value=0x%x, cached_dt_value=0x%x, cached_fps_value=%u, framerate=%u\n",
sensor->sd.name, dt_value, sensor->cached_dt_value, sensor->cached_fps_value, sensor->config.framerate);

Copilot uses AI. Check for mistakes.

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I agree

Comment thread kernel/realsense/d4xx.c
u16 height_value = 0;
int ret;

current_reset_gen = atomic_read(&ds5_reset_gen);

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

reset_gen is shared between the instances ?

@ymodlin
ymodlin merged commit 445be76 into dev Feb 25, 2026
7 checks passed
@ymodlin
ymodlin deleted the FIX_DS5_HW_reset branch February 25, 2026 07:46
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants